/**
 * Created by vicket on 2017/4/14.
 */
$(function(){
    //返回上个页面
    var back=$(".back");
    back.attr("href",document.referrer);
    //获取用户名
    var userId=sessionStorage.getItem("userId");
    //获取小说信息
    var novelId=window.location.hash.slice(1);
    var novleInfo=$(".novel_info");
    var backgroundImg=$(".background_img");
    var briefContent=$(".brief_content");
    $.ajax({
        type:"get",
        url:"/api/frontGetBookDetail",
        data:{bookId:novelId},
        success:function(result){
            console.log(result);
            //存储小说名
            var novelId=result[0].bookId;
            sessionStorage.setItem("bookId",result[0].bookId);
            //设置小说背景
            backgroundImg.attr("src",result[0].imgUrl);
            //获取小说简介
            briefContent.text(result[0].introduct);
            //获取小说信息
            var html=template("novelDetail",{"items":result});
            novleInfo.html(html);
           //操作简介展开与收起
            var openBrief=$(".open_brief");
            var isOpen=true;
            openBrief.on("touchstart",function(){
                if(isOpen){
                    isOpen=false;
                    openBrief.text("收起")
                    openBrief.addClass("current");
                    briefContent.css("height","auto");
                }else{
                    isOpen=true;
                    openBrief.text("展开");
                    openBrief.removeClass("current");
                    briefContent.css("height","48px");
                }
            });
            //写点评
            var writeComment=$(".write_comment");
            if(userId){
                writeComment.attr("href","writecomment.ejs#"+novelId);
            }
            //获取评论数据
            var commentList=$(".comment_list");
            $.ajax({
                type:"get",
                url:"/api/frontGetComment",
                data:{bookId:novelId},
                success:function(result){
                    var html=template("commentList",{"items":result});
                    commentList.html(html);
                    //点赞功能
                    var upvote=$(".upvote");
                    for(var i=0;i<upvote.length;i++){
                        upvote[i].index=i;
                    }
                    upvote.on("touchstart",function(){
                        if(userId){
                            var _this=this;
                            $.ajax({
                                type:"get",
                                url:"/api/addGood",
                                data:{auditId:result[_this.index].auditId},
                                success:function(res){
                                    $.ajax({
                                        type:"get",
                                        url:"/api/frontGetComment",
                                        data:{bookId:novelId},
                                        success:function(result){
                                            $(_this).text(result[_this.index].good);
                                        }
                                    })
                                }
                            })
                        }else{
                            maskText.text("请登录后再点赞");
                            maskTip.css("display","block")
                            setTimeout(function(){
                                maskTip.css("display","none");
                            },2000);
                        }
                        })



                }
            });
            //目录跳转
             var novelLista=$(".novel_lista");
            novelLista.attr("href","novelcatalog.ejs#"+result[0].bookId);

        }
    });

    //加入书架
    var novelAdd=$(".novel_add");
    var windowHeight=$(window).height();
    var maskTip=$(".mask_tip");
    maskTip.height(windowHeight);
    maskTip.css("display","none");
    var maskText=$(".mask_content p");
    novelAdd.click(function() {
        var bookId = sessionStorage.getItem("bookId");
        if (userId) {
            $.ajax({
                type: "post",
                url: "/api/frontAddToBookcase",
                data: {bId: bookId, uId: userId},
                success: function (result) {
                    console.log(result);
                    if (result.code == 1) {
                        maskText.text(result.message);
                        maskTip.css("display","block")
                        setTimeout(function(){
                            maskTip.css("display","none");
                        },2000)
                    } else {
                        maskText.text(result.message);
                        maskTip.css("display","block")
                        setTimeout(function(){
                            maskTip.css("display","none");
                        },2000);
                    }
                }
            });
        } else {
            maskText.text("您还未登录，收藏该书籍");
            maskTip.css("display","block")
            setTimeout(function(){
                maskTip.css("display","none");
            },2000);
        }
    });








})